<?php
namespace app\bis\controller;

use app\common\facade\BisAccount;
use think\Controller;
use think\facade\Session;
use think\Request;

class Login extends Controller
{
    public function index(Request $request)
    {
        if ($request->isPost())
        {
            // 登录逻辑
            $data = $request->post();
            //通过用户获取用户相关信息
            // TODO:数据校验
            $account = model('BisAccount')->get(['username' => $data['username']]);
            if (!$account || $account->status != 1)
            {
                $this->error('该用户不存在或未被审核通过');
            }
            if ($account->password != md5($data['password'].$account->code))
            {
                $this->error('密码错误');
            }
            model('BisAccount')->updateById(['last_login_time' => time(), 'last_login_ip' => $request->ip()], $account->id);
            // 保存用户信息
            Session::set('bisAccount', $account, 'bis');
            return $this->success('登录成功', 'index/index');
        }
        else
        {
            // 获取 session
            $account = Session::get('bisAccount', 'bis');
            if ($account && $account->id)
            {
                $this->redirect('index/index');
            }
            return $this->fetch();
        }

    }

    public function logout()
    {
        Session::clear('bis');
        $this->redirect('login/index');
    }
}